﻿using Bilibili.Model.Resource;
using Newtonsoft.Json.Linq;
using PCLUntils.Objects;
using System.Collections.Generic;

namespace Bilibili.Common.Untils
{
    public abstract class BaseSourceAdapter
    {
        public string ErrorMessage { get; protected set; }
        public List<string> MediaUrls { get; protected set; }
        protected bool GetDashInfo(JObject obj, out long duration, out long minBufferTime)
        {
            bool hasData = false;
            try
            {
                duration = obj["data"]["dash"]["duration"].ToInt64();
                minBufferTime = obj["data"]["timelength"].ToInt64();
                hasData = true;
            }
            catch
            {
                try
                {
                    duration = obj["result"]["dash"]["duration"].ToInt64();
                    minBufferTime = obj["result"]["timelength"].ToInt64();
                    hasData = true;
                }
                catch
                {
                    try
                    {
                        duration = obj["dash"]["duration"].ToInt64();
                        minBufferTime = obj["timelength"].ToInt64();
                        hasData = true;
                    }
                    catch
                    {
                        duration = 0;
                        minBufferTime = 0;
                    }
                }
            }
            return hasData;
        }
        protected static bool GetDashItem(JObject obj, out List<DashItem> videos, out List<DashItem> audios)
        {
            bool hasData = false;
            try
            {
                videos = obj["data"]["dash"]["video"].ToObject<List<DashItem>>();
                audios = obj["data"]["dash"]["audio"].ToObject<List<DashItem>>();
                hasData = true;
            }
            catch
            {
                try
                {
                    videos = obj["result"]["dash"]["video"].ToObject<List<DashItem>>();
                    audios = obj["result"]["dash"]["audio"].ToObject<List<DashItem>>();
                    hasData = true;
                }
                catch
                {
                    try
                    {
                        videos = obj["dash"]["video"].ToObject<List<DashItem>>();
                        audios = obj["dash"]["audio"].ToObject<List<DashItem>>();
                        hasData = true;
                    }
                    catch
                    {
                        videos = null;
                        audios = null;
                    }
                }
            }
            return hasData;
        }
    }
}